home *** CD-ROM | disk | FTP | other *** search
- Path: in2.uu.net!zdc!szdc!news
- From: braz@ime.usp.br (Rodrigo de Salvo Braz)
- Newsgroups: comp.lang.c++
- Subject: Re: Why? Virtual member causes General Protection Exception
- Date: Mon, 15 Apr 1996 17:05:34 GMT
- Organization: Zippo
- Message-ID: <4ku0vr$lg3@clark.zippo.com>
- References: <31730432.28C@es.flinders.edu.au>
- NNTP-Posting-Host: ddata116.dialdata.com.br
- X-Newsreader: Forte Free Agent 1.0.82
-
- "T. Gruber" <gruber@es.flinders.edu.au> wrote:
-
- >Hi
-
- >I can compile and run the appended code (BC++ 4.5; Win 3.11), but it causes a
- >General Protection Exception in the last line. Why?
-
- >A::y() does call B::x(), but this code does not get the right address for B::i. I
- >assume I am at fault, but I can't find a documented reference that tells me so. Am
- >*I* stupid?
-
- >Please email a copy of your reply.
-
- >Thanks
-
- >Thomas
-
- >#include <iostream.h>
-
- >class A {
- >public:
- > virtual void x() = 0;
- > void y() { x(); };
- >};
-
- >class B : public virtual A {
- >public:
- > B() : i(0) {};
- > virtual void x() { cout << i << endl; };
- > int i;
- >};
-
- >class C : public virtual A, public virtual B {};
-
- >void main()
- >{
- > C c1;
- > C c2(c1);
- > c2.x(); // works
- > c2.y(); // crashes in B::x()
- >}
-
- If you had this problem this exactly way, I don't know how to help
- you. But if the code is divided in modules, I have had the same kind
- of problem. The fact is that virtual tables, which contain addresses
- for virtual member functions, get those addresses only at run-time. So
- you begin you program execution without correct addresses there! When
- you get at main function, you have them already, but by now some
- constructor may have been run and crashed if it tried to use those
- virtual member function.
-
- Good luck,
-
- Rodrigo Braz
-
-
-